package com.dys.testjdk12;

/**
 * @className: com.dys.testjdk12.SwitchTest
 * @description: com.dys.testjdk12
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/08 下午8:08
 */
public class SwitchTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        // testJava8Switch();
        // testJava12Switch();
        testJava14Switch();
    }

    /**
     * 根据传递的月份输出对应的季节
     */
    private static void testJava8Switch() {
        Month month = Month.JANUARY;
        String season;
        switch (month) {
            case JANUARY:
            case DECEMBER:
            case FEBRUARY:
                season = "冬天";
                break;
            case MARCH:
            case APRIL:
            case MAY:
                season = "春天";
                break;
            case JUNE:
            case JULY:
            case AUGUST:
                season = "夏天";
                break;
            case SEPTEMBER:
            case OCTOBER:
            case NOVEMBER:
                season = "秋天";
                break;
            default:
                throw new RuntimeException("选择的月份错误");

        }
        System.out.println("现在是：" + season);
    }

    /**
     * Java12之后的写法
     */
    private static void testJava12Switch() {
        Month month = Month.AUGUST;
        String season;
        switch (month) {
            case DECEMBER, JANUARY, FEBRUARY -> season = "冬天";
            case MARCH, APRIL, MAY -> season = "春天";
            case JUNE, JULY, AUGUST -> season = "夏天";
            case SEPTEMBER, OCTOBER, NOVEMBER -> season = "秋天";
            default -> throw new RuntimeException("输入月份错误");
        }
        System.out.println("现在的季节是：" + season);
    }

    /**
     * Java14之后的写法
     */
    private static void testJava14Switch() {
        String str = "1";
        String info = switch (str) {
            case "1" -> {
                yield "message1";
            }
            case "2" -> {
                yield "message2";
            }
            case "3" -> {
                yield "message3";
            }
            default -> {
                yield "no message";
            }
        };
        System.out.println(info);
    }
}
